package com.rc.evcharger.common.utils.log;

import java.io.*;

/**
 * 筛选日志
 * 2017年12月22日
 *
 * @author peter
 */
public class SelChargeProcessLogUtil {

    //public static final String filePah = "E:\\selReport.txt";
    /**
     * 默认读取路劲
     */
    public static final String FILEPATH = "E:\\runtime\\logs\\";

    public static final String SPLIT = " ";

    /**
     * @param fileName 日志名;
     * @param orderNum 需要查询的字符串;
     */
    public static String getSelReport(String fileName, String orderNum,String tempFileName) throws Exception {
        //orderNum = orderNumHandle(orderNum);
        //fileName = filePathHandle(fileName);
        StringBuilder sb = new StringBuilder();
        BufferedReader reader = null;
        BufferedWriter writer = null;
        String temp = null;
        int line = 1;
        try {
            writer = new BufferedWriter(new FileWriter(tempFileName));
            reader = new BufferedReader(new InputStreamReader(new FileInputStream(fileName), "UTF-8"));
            while ((temp = reader.readLine()) != null) {
                if (temp.indexOf(orderNum) != -1) {
                    //System.out.println(line + ":" + temp + "\n");
                    writer.write(line + ":" + temp + "\r\n\r\n");
                    sb.append(line + ":" + temp + "\n");
                    line++;
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (reader != null) {
                reader.close();
            }
            if (writer != null) {
                writer.close();
            }

        }
        return sb.toString();
    }


//    public static String orderNumHandle(String orderNum) {
//        StringBuilder sb = new StringBuilder();
//        String number = "";
//        if (orderNum.length() > 8) {
//            number = numSerial(orderNum.substring(orderNum.length() - 8, orderNum.length()));
//            orderNum = orderNum.substring(0, orderNum.length() - 8);
//        }
//        char[] chars = orderNum.toCharArray();
//        for (char c : chars) {
//            sb.append(WebConstant.CON0).append(c).append(SPLIT);
//        }
//        System.out.println(sb.toString());
//        return sb.toString().trim() + SPLIT + number;
//    }


    public static String orderNumHandle(String orderNum) {
        StringBuilder sb = new StringBuilder();
        if (orderNum.length() % 2 != 0) {
            orderNum = orderNum.substring(1, orderNum.length());
        }

        char[] chars = orderNum.toCharArray();
        sb.delete(0, sb.length());
        for (int i = 0; i < chars.length; i = i + 2) {
            sb.append("" + chars[i] + chars[i + 1] + SPLIT);
        }
        return sb.toString().trim();
    }


    public static String numSerial(String number) {
        String a = "";
        char[] chars = number.toCharArray();
        for (int i = 0; i < chars.length; i++) {
            if (i % 2 == 1) {
                a += chars[i] + SPLIT;
            } else {
                a += chars[i];
            }
        }
        return a.trim();
    }

    public static String filePathHandle(String fileName) {
        return FILEPATH + fileName;
    }

    public static void main(String[] args) {                         //001591058447800237891
        //getSelReport("E:\\testWord\\25\\jinzhuang_server_command.log.23", "01 03 07 01 00 02 00 01 02 01 06");
//        try {
//            getSelReport("jinzhuang_server_command.log.78", "1802856489200001496");
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
//        System.out.println(SelOrderNumUtil.orderNumHandle1("04401190109182203190013610771"));

    }
}
